package com.shareppy.utils;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class StringUtils {

	/**
	 * Valida si una cadena es nula o vacía
	 * 
	 * @param string
	 *            La cadena a evaluar
	 * @return true si la cadena es nula o vacía, false en caso contrario.
	 */
	public static boolean isEmpty(String string) {
		return string == null || string.trim().length() == 0;
	}

	public static boolean isAllEmpty(String... strings) {
		if (strings != null) {
			for (String string : strings) {
				if (!isEmpty(string)) {
					return false;
				}
			}
			return true;
		}
		throw new RuntimeException("At least one parameter is required");
	}
	
	public static boolean isAnyEmpty(String... strings) {
		if (strings != null) {
			for (String string : strings) {
				if (isEmpty(string)) {
					return true;
				}
			}
			return false;
		}
		throw new RuntimeException("At least one parameter is required");
	}

	public static Map<String, String> string2Hash(Object response) {
		return string2Hash((String) response);
	}

	public static Map<String, String> string2Hash(String response) {
		Map<String, String> map = new HashMap<String, String>();
		String[] rows = response.split("\n");
		for (int i = 0; i < rows.length; i++) {
			String[] cols = rows[i].split("=");
			if (cols.length == 2) {
				map.put(cols[0], cols[1]);
			}
		}
		return map;
	}

	public static Map<String, String> string2LinkedHash(Object response) {
		return string2LinkedHash((String) response);
	}

	public static Map<String, String> string2LinkedHash(String response) {
		Map<String, String> map = new LinkedHashMap<String, String>();
		String[] rows = response.split("\n");
		for (int i = 0; i < rows.length; i++) {
			String[] cols = rows[i].split("=");
			if (cols.length == 2) {
				map.put(cols[0], cols[1]);
			}
		}
		return map;
	}

	public static String addSpces(String string, int size, boolean atLeft,
			char charvalue) {
		if (string == null) {
			string = "";
		}
		int slength = string.length();
		if (slength > size) {
			return string.substring(0, slength);
		}
		StringBuffer buffer = new StringBuffer(string);
		for (int i = slength; i < size; i++) {
			if (atLeft) {
				buffer.insert(0, charvalue);
			} else {
				buffer.append(charvalue);
			}
		}
		return buffer.toString();
	}

	public static String addSpces(String string, int size, boolean atLeft) {
		return addSpces(string, size, atLeft, ' ');
	}

	public static String formatNumber(String text) {
		if (StringUtils.isEmpty(text)) {
			return "";
		}
		char[] charArray = text.toCharArray();
		StringBuffer sb = new StringBuffer("");
		int i = charArray.length - 1;
		int counter = 1;
		while (i >= 0) {
			sb.insert(0, charArray[i]);
			if (counter % 3 == 0 && i > 0) {
				sb.insert(0, ',');
			}
			i--;
			counter++;
		}
		return sb.toString();
	}

	public static String unformat(String text) {
		return text.replaceAll(",", "");
	}

	public static int getValueLength(int labelLength, int valueLength) {
		return getValueLength(32, labelLength, valueLength);
	}

	public static int getValueLength(int charsByLine, int labelLength,
			int valueLength) {
		int vlength = charsByLine - labelLength;
		while (vlength < valueLength) {
			vlength += charsByLine;
		}
		return vlength;
	}

	public static <T> T nvl(T value, T valueIfIsNull) {
		if (value == null) {
			return valueIfIsNull;
		}
		return value;
	}

	public static String toString(Object object) {
		if (object == null) {
			return null;
		}
		if (object instanceof Map<?, ?>) {
			return toString((Map<?, ?>) object);
		}
		return object.toString();
	}

	public static String nvl(String value) {
		return value == null ? "" : value;
	}
}